package com.tlx.app.dedao;

import java.io.File;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FilenameUtils;

import com.tlx.core.util.TFileUtils;
import com.tlx.pdf.PdfOperateUtils;

public class PdfGroupBean extends GroupBean {

    public PdfGroupBean(String gi, String gn, String srcDir, String lastDir) {
        super(gi, gn, srcDir, lastDir);
    }

    protected void initPdf() {
        List<String> pdfList = TFileUtils.getFileListByExt(this.srcDir, "pdf");
        for (String pdf : pdfList) {
            if (FilenameUtils.getBaseName(pdf).startsWith(gid)) {
                srcPdfList.add(pdf);
            }
        }
        this.lastPdf = new File(this.lastDir, gid + gname + ".pdf").getAbsolutePath();
        if (CollectionUtils.isNotEmpty(srcPdfList) && new File(lastPdf).exists()) {
            throw new RuntimeException("目标文件存在：" + "【" + lastPdf + "】");
        }
    }

    @Override
    public void genPdf() {
        if (CollectionUtils.isNotEmpty(srcPdfList)) {
            PdfOperateUtils.mergePdf(srcPdfList, lastPdf);
            TFileUtils.deleteListQuietly(srcPdfList);
        }
    }

}
